﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Hedgehog
{
    class BezierCurve : IFigure
    {
        public int offsetX { get; set; }
        public int offsetY { get; set; }
        private int _x1;
        private int _x2;
        private int _x3;
        private int _y1;
        private int _y2;
        private int _y3;
        

        public BezierCurve() { }

        public BezierCurve(int X1,int Y1,int X2,int Y2,int X3,int Y3)
        {
            _x1 = X1;
            _x2 = X2;
            _x3 = X3;
            _y1 = Y1;
            _y2 = Y2 ;
            _y3 = Y3;
         
        }

        public virtual void Draw(Graphics g)
        {
            bool firstIteration = true;
            float lastX = 0;
            float lastY = 0;
            Random random = new Random();
            int count = 0;
            {
                for (float t = 0; t <= 1; t += (float) 0.01)
                {
                    float newX;
                    float newY;
                    newX = F(t, _x1 + offsetX, _x2 + offsetX, _x3 + offsetX);
                    newY = F(t, _y1 + offsetY, _y2 + offsetY, _y3 + offsetY);

                    if (firstIteration)
                    {
                        lastX = newX;
                        lastY = newY;
                        firstIteration = false;
                        continue;
                    }



                    g.DrawLine(Pens.Black, lastX, lastY, newX, newY);

                    count++;

                    lastX = newX;
                    lastY = newY;
                }



            }
        }


        public virtual bool ChekIfPointInside(int x, int y)
        {
            return TriangleHelper.CheckIfPointInsideTriangle(new Point(_x1 + offsetX, _y1 + offsetY),
                                                             new Point(_x2 + offsetX, _y2 + offsetY),
                                                             new Point(_x3 + offsetX, _y3 + offsetY), new Point(x, y));
        }

        protected float F(float t, int a, int b, int c)
        {
            return (1-t)*(1-t)*a+2*t*(1-t)*b+t*t*c;
            
        }

        public virtual object Clone()
        {
            return new BezierCurve(_x1,_y1,_x2,_y2,_x3,_y3);
        }
    }
    }

 